<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
				<div class="right-panel-search" style="margin-left: 10px;">
					<el-select style="width: 100px;" v-model="search.status" clearable
						placeholder="状态" filterable>
						<el-option v-for="(item, key) in statuss" :key="key" :label="item" :value="key" />
					</el-select>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</div>
			<div class="right-panel">
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="list.apiObj" row-key="id" stripe>
				<el-table-column label="充值金额" prop="amount" width="150"></el-table-column>
				<el-table-column label="赠送金额" prop="give" width="150"></el-table-column>
				<el-table-column label="状态" prop="status" width="50">
					<template #default="scope">
						<p >{{scope.row.status == 1 ? '启用' : '禁用'}}</p>
					</template>
				</el-table-column>
				<el-table-column label="排序" prop="sort" width="150"></el-table-column>
				<el-table-column label="创建时间" prop="create_time" width="150"></el-table-column>
				<el-table-column label="操作" fixed="right" align="right" width="100">
					<template #default="scope">
						<el-button type="primary" plain size="small" @click="table_edit(scope.row)">编辑</el-button>
						<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
							<template #reference>
								<el-button text type="primary" size="small">删除</el-button>
							</template>
						</el-popconfirm>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>


</template>

<script>
	import saveDialog from './save'

	export default {
		name: 'configureList',
		components: {
			saveDialog,
		},
		data() {
			return {
				dialog:{
					save: false,
				},
				search: {
					status: '',
				},
				statuss: {'1': '显示', '0': '隐藏'},
				list: {
					apiObj: this.$API.fund.BalanceRecharge.list
				},
			}
		},
		mounted() {

		},
		methods: {
			//删除
			async table_del(row){
				var reqData = {id: row.id}
				var res = await this.$API.fund.BalanceRecharge.delete.post(reqData);
				if(res.code == 200){
					this.$refs.table.refresh()
					this.$message.success("删除成功")
				}else{
					this.$alert(res.msg, "提示", {type: 'error'})
				}
			},
			//搜索
			upsearch() {
				// console.log(112, this.search)
				let search = JSON.parse(JSON.stringify(this.search))
				this.$refs.table.upData({
					...search
				})
			},
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			//窗口编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},

			//本地更新数据
			handleSaveSuccess(data, mode){
				//为了减少网络请求，直接变更表格内存数据
				if(mode=='add'){
					this.$refs.table.unshiftRow(data)
				}else if(mode=='edit'){
					this.$refs.table.updateKey(data)
				}

				//当然也可以暴力的直接刷新表格
				// this.$refs.table.refresh()
			}
		}
	}
</script>

<style>
</style>
